﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace Delegate1
{
    static class Program
    {
        /// <summary>
        /// 應用程式的主要進入點。
        /// </summary>
        
        public class MyClass
        {
            //1.建立方法==================================================================
            public int InstanceMethod(int a, int b)
            {
                Console.WriteLine("Call InstanceMethod ");
                Console.WriteLine("First parametre is {0},second parametre is {1}", a, b);
                return 0;
            }
            static public void StaticMethod()
            {
                Console.WriteLine("Call Static Method");
                
            }
            //1.建立方法==================================================================
        }
        //2.宣告委派======================================================================
        //委派的參數，需要與被呼叫的方法一樣，包含回傳型別
        private delegate void MyDelegate();
        private delegate int MeDelegatePara(int a, int b);
        //2.宣告委派======================================================================
        [STAThread]
        static void Main()
        {
            //3.引用委派(實體化)，後指定方法
            MyDelegate D = new MyDelegate(MyClass.StaticMethod);
            //4.呼叫函數
            D.Invoke();

            MyClass p = new MyClass();
            //3.引用委派(實體化)，後指定方法
            MeDelegatePara d = new MeDelegatePara(p.InstanceMethod);
            //4.呼叫函數
            d.Invoke(1, 2);      
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());

        }


    }
}